<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use Cache;
use Intervention\Image\ImageManagerStatic as Image;

class ProductAttrController extends Controller
{
    public function index(Request $request,$id)
    {
        $inputs=$request->except(['_token','_method']);
        //var_dump($inputs);
        $this->validate($request,[
            'title'=>'required|min:4|max:12',
            'mprice'=>'required|numeric',
            'sprice'=>'required|numeric',
            'lprice'=>'numeric'        
        ],[
            'title.required'=>'商品标题不能为空',
            'title.min'=>'商品标题长度必须4-12字符',
            'title.max'=>'商品标题长度必须4-12字符',
            'mprice.required'=>'市场价不能为空',
            'sprice.required'=>'商城价不能为空',
            'numeric'=>'价格必须数字'
        ]);
        if($request->hasFile('logo_pic')){
            if($request->file('logo_pic')->isValid())
            {
                $logo_filename=time().rand(1000,9999);
                $logo_ext=$request->file('logo_pic')->getClientOriginalExtension();
                $logo_basename=$logo_filename.'.'.$logo_ext;
                $request->file('logo_pic')->move('uploads/product/logo_pic',$logo_basename);
                //等比缩放
                $fname='uploads/product/logo_pic/'.$logo_basename;
                //var_dump($fname);
                $srcarr = getimagesize($fname);
                //var_dump($srcarr);
                $w=100;
                $h=100;
                if($w/$srcarr[0]>$h/$srcarr[1]){
                    $bili = $h/$srcarr[1];
                }else{
                    $bili = $w/$srcarr[0];
                }
                $nw = floor($srcarr[0]*$bili);
                $nh = floor($srcarr[1]*$bili);
                $img = Image::make($fname)->resize($nw,$nh)->save($fname);//缩放
                //var_dump($img);
                if(file_exists('./uploads/product/logo_pic/'.$inputs['old_logo_pic'])){
                    unlink('./uploads/product/logo_pic/'.$inputs['old_logo_pic']);
                }
            }   
        }else{
            $logo_basename=$inputs['old_logo_pic'];
        }
        //var_dump($logo_basename);

        if($request->hasFile('ad_pic')){
            if($request->file('ad_pic')->isValid())
            {
                $ad_filename=time().rand(1000,9999);
                $ad_ext=$request->file('ad_pic')->getClientOriginalExtension();
                $ad_basename=$ad_filename.'.'.$ad_ext;
                $request->file('ad_pic')->move('uploads/product/ad_pic',$ad_basename);
                //等比缩放
                $fname='uploads/product/ad_pic/'.$ad_basename;
                //var_dump($fname);
                $srcarr = getimagesize($fname);
                //var_dump($srcarr);
                $w=300;
                $h=300;
                if($w/$srcarr[0]>$h/$srcarr[1]){
                    $bili = $h/$srcarr[1];
                }else{
                    $bili = $w/$srcarr[0];
                }
                $nw = floor($srcarr[0]*$bili);
                $nh = floor($srcarr[1]*$bili);
                $img = Image::make($fname)->resize($nw,$nh)->save($fname);//缩放
                //var_dump($img);
                 if($inputs['old_ad_pic']!=''){
                    if(file_exists('./uploads/product/ad_pic/'.$inputs['old_ad_pic'])){
                    unlink('./uploads/product/ad_pic/'.$inputs['old_ad_pic']);
                    }
                }

            }   
        }else{
            $ad_basename=$inputs['old_ad_pic'];
        }
        //var_dump($ad_basename);
        DB::table('product')->where('product_id',$id)->update(['title'=>$inputs['title'],'description'=>$inputs['description'],'mprice'=>$inputs['mprice'],'sprice'=>$inputs['sprice'],'lprice'=>$inputs['lprice'],'new_recommended'=>$inputs['new_recommended'],'hotsell_recommended'=>$inputs['hotsell_recommended'],'boutique_recommended'=>$inputs['boutique_recommended'],'disabled'=>$inputs['disabled'],'logo_pic'=>$logo_basename,'ad_pic'=>$ad_basename]);

        //显示属性列表
        $attr=DB::table('pro_attr')->leftJoin('col_siz_rel','col_siz_rel.cosir_id','=','pro_attr.cosir_id')->leftJoin('color','color.color_id','=','col_siz_rel.col_id')->leftJoin('size','size.size_id','=','col_siz_rel.siz_id')->where('pro_attr.pro_id','=',$id)->get();
        //var_dump($attr);
        return view('admin/product/product/indexattr',['attr'=>$attr,'pro_id'=>$id]);


    }
    //添加商品属性
    public function create($id)
    {
        //var_dump($id);
        //色系
        //属性已有
        $cosir_ids=DB::table('pro_attr')->where('pro_id','=',$id)->lists('cosir_id');
        //var_dump($cosir_ids);
        //查已有颜色ID
        $color_ids=DB::table('col_siz_rel')->whereIn('cosir_id',$cosir_ids)->lists('col_id');
        //var_dump($color_ids);
        //去重
        $color_ids=array_unique($color_ids);
        //var_dump($color_ids);
        $colors=DB::table('color')->whereNotIn('color_id',$color_ids)->get();
        //var_dump($colors);
        //dd($colors);

        //尺码(按人群查)
        $crspr_id=DB::table('product')->where('product_id','=',$id)->lists('crspr_id');
        $cro_id=DB::table('cro_spo_rel')->where('crspr_id','=',$crspr_id)->lists('cro_id');

        //var_dump($cro_id);
        if($cro_id[0]==1){
            $type='cro_m_con';
        }elseif($cro_id[0]==2){
            $type='cro_w_con';
        }elseif($cro_id[0]==3){
            $type='cro_c_con';
        }

        $sizes=DB::table('size')->where($type,'=','1')->select('size_id','size_size')->get();
        //var_dump($msizes);
        $sizes_str='';
            foreach($sizes as $size){
                $every_size='<div class="form-group"><label for="" class="col-sm-2 col-md-2 control-label"></label><div class="col-sm-2 col-md-1"><input type="checkbox" name="size[]" value="'.$size->size_id.'">'.$size->size_size.'</div><div class="col-sm-8 col-md-7"><input type="text" name="attr_stocknum[]" disabled="true" value="0"/></div></div>';
                $sizes_str.=$every_size;
            }
            //var_dump($sizes_str);

        return view('admin/product/product/createattr',['colors'=>$colors,'sizes_str'=>$sizes_str,'pro_id'=>$id]);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $inputs=$request->except(['_token']);
        //var_dump($inputs);
        $this->validate($request,[
            'size'=>'required',
            'carouse_pic1'=>'required|image',
            'carouse_pic2'=>'required|image',
            'carouse_pic3'=>'required|image',
            'carouse_pic4'=>'required|image',

        ],[
            'size.required'=>'必须选择一种尺码',
            'carouse_pic1.required'=>'轮播必须四个',
            'carouse_pic2.required'=>'轮播必须四个',
            'carouse_pic3.required'=>'轮播必须四个',
            'carouse_pic4.required'=>'轮播必须四个',
            'carouse_pic1.image'=>'必须图片类型',
            'carouse_pic2.image'=>'必须图片类型',
            'carouse_pic3.image'=>'必须图片类型',
            'carouse_pic4.image'=>'必须图片类型'
        ]);
        //属性关系ID数组
        $cosir_id_arr=array();
        //属性关键字数组
        $keyword_arr=array();
        //后置商品编号数组
        $suf_pro_code_arr=array();
        foreach($inputs['size'] as $v){
            //var_dump($v);    
            $this_cosir_id=DB::table('col_siz_rel')->where('col_id','=',$inputs['col_id'])->where('siz_id','=',$v)->value('cosir_id');
            $cosir_id_arr[]=$this_cosir_id;

            $this_color=DB::table('color')->where('color_id','=',$inputs['col_id'])->first();
            $this_size=DB::table('size')->where('size_id','=',$v)->first();
            $this_keyword=$this_color->color_name_zh.'-欧码'.$this_size->size_size.'号';
            $keyword_arr[]=$this_keyword;
            $this_suf_pro_code=$this_color->color_pro_code.$this_size->size_pro_code;
            $suf_pro_code_arr[]=$this_suf_pro_code;
            //var_dump($this_color);
            //var_dump($this_size);
        }
        //var_dump($cosir_id_arr);
        //var_dump($keyword_arr);
        //var_dump($suf_pro_code_arr);
        //轮播图片1
        $cp_filename=time().rand(1000,9999);
        if($request->file('carouse_pic1')->isValid())
        {
            $cp_ext=$request->file('carouse_pic1')->getClientOriginalExtension();
            $cp_basename=$cp_filename.'_1.'.$cp_ext;
            $request->file('carouse_pic1')->move('uploads/product/carouse_pic',$cp_basename);
            //等比缩放
            $fname='uploads/product/carouse_pic/'.$cp_basename;
            //var_dump($fname);
            $srcarr = getimagesize($fname);
            //var_dump($srcarr);
            $w=400;
            $h=400;
            if($w/$srcarr[0]>$h/$srcarr[1]){
                $bili = $h/$srcarr[1];
            }else{
                $bili = $w/$srcarr[0];
            }
            $nw=floor($srcarr[0]*$bili);
            $nh=floor($srcarr[1]*$bili);
            $img=Image::make($fname)->resize($nw,$nh)->save($fname);
            //缩放
            //var_dump($img);
        }
        if($request->file('carouse_pic2')->isValid())
        {
            $cp_ext=$request->file('carouse_pic2')->getClientOriginalExtension();
            $cp_basename=$cp_filename.'_2.'.$cp_ext;
            $request->file('carouse_pic2')->move('uploads/product/carouse_pic',$cp_basename);
            //等比缩放
            $fname='uploads/product/carouse_pic/'.$cp_basename;
            //var_dump($fname);
            $srcarr = getimagesize($fname);
            //var_dump($srcarr);
            $w=400;
            $h=400;
            if($w/$srcarr[0]>$h/$srcarr[1]){
                $bili = $h/$srcarr[1];
            }else{
                $bili = $w/$srcarr[0];
            }
            $nw=floor($srcarr[0]*$bili);
            $nh=floor($srcarr[1]*$bili);
            $img=Image::make($fname)->resize($nw,$nh)->save($fname);
            //缩放
            //var_dump($img);
        }
        if($request->file('carouse_pic3')->isValid())
        {
            $cp_ext=$request->file('carouse_pic3')->getClientOriginalExtension();
            $cp_basename=$cp_filename.'_3.'.$cp_ext;
            $request->file('carouse_pic3')->move('uploads/product/carouse_pic',$cp_basename);
            //等比缩放
            $fname='uploads/product/carouse_pic/'.$cp_basename;
            //var_dump($fname);
            $srcarr = getimagesize($fname);
            //var_dump($srcarr);
            $w=400;
            $h=400;
            if($w/$srcarr[0]>$h/$srcarr[1]){
                $bili = $h/$srcarr[1];
            }else{
                $bili = $w/$srcarr[0];
            }
            $nw=floor($srcarr[0]*$bili);
            $nh=floor($srcarr[1]*$bili);
            $img=Image::make($fname)->resize($nw,$nh)->save($fname);
            //缩放
            //var_dump($img);
        }
        if($request->file('carouse_pic4')->isValid())
        {
            $cp_ext=$request->file('carouse_pic4')->getClientOriginalExtension();
            $cp_basename=$cp_filename.'_4.'.$cp_ext;
            $request->file('carouse_pic4')->move('uploads/product/carouse_pic',$cp_basename);
            //等比缩放
            $fname='uploads/product/carouse_pic/'.$cp_basename;
            //var_dump($fname);
            $srcarr = getimagesize($fname);
            //var_dump($srcarr);
            $w=400;
            $h=400;
            if($w/$srcarr[0]>$h/$srcarr[1]){
                $bili = $h/$srcarr[1];
            }else{
                $bili = $w/$srcarr[0];
            }
            $nw=floor($srcarr[0]*$bili);
            $nh=floor($srcarr[1]*$bili);
            $img=Image::make($fname)->resize($nw,$nh)->save($fname);
            //缩放
            //var_dump($img);
        }
        //var_dump($cosir_id_arr);
        //var_dump($keyword_arr);
        //var_dump($suf_pro_code_arr);
        //var_dump($cp_filename);
        //var_dump(count($cosir_id_arr));
        for($i=0;$i<=count($cosir_id_arr)-1;$i++){
            DB::table('pro_attr')->insert(['pro_id'=>$inputs['pro_id'],'cosir_id'=>$cosir_id_arr[$i],'keyword'=>$keyword_arr[$i],'suf_pic'=>$cp_filename,'attr_stocknum'=>$inputs['attr_stocknum'][$i],'suf_pro_code'=>$suf_pro_code_arr[$i]]);
        }
        //此色系总库存
        //var_dump($inputs['attr_stocknum']);
        $attr_stocknums=array_sum($inputs['attr_stocknum']);
        //var_dump($attr_stocknums);
        DB::table('product')->increment('stocknum',$attr_stocknums);
        return redirect('admin/product')->with('add_pro_success','添加商品成功');
        
    }

    //继续添加属性
    public function storenext(Request $request)
    {

        $inputs=$request->except(['_token']);
        //var_dump($inputs);
        $this->validate($request,[
            'size'=>'required',
            'carouse_pic1'=>'required|image',
            'carouse_pic2'=>'required|image',
            'carouse_pic3'=>'required|image',
            'carouse_pic4'=>'required|image',

        ],[
            'size.required'=>'必须选择一种尺码',
            'carouse_pic1.required'=>'轮播必须四个',
            'carouse_pic2.required'=>'轮播必须四个',
            'carouse_pic3.required'=>'轮播必须四个',
            'carouse_pic4.required'=>'轮播必须四个',
            'carouse_pic1.image'=>'必须图片类型',
            'carouse_pic2.image'=>'必须图片类型',
            'carouse_pic3.image'=>'必须图片类型',
            'carouse_pic4.image'=>'必须图片类型'
        ]);
        //属性关系ID数组
        $cosir_id_arr=array();
        //属性关键字数组
        $keyword_arr=array();
        //后置商品编号数组
        $suf_pro_code_arr=array();
        foreach($inputs['size'] as $v){
            //var_dump($v);    
            $this_cosir_id=DB::table('col_siz_rel')->where('col_id','=',$inputs['col_id'])->where('siz_id','=',$v)->value('cosir_id');
            $cosir_id_arr[]=$this_cosir_id;

            $this_color=DB::table('color')->where('color_id','=',$inputs['col_id'])->first();
            $this_size=DB::table('size')->where('size_id','=',$v)->first();
            $this_keyword=$this_color->color_name_zh.'-欧码'.$this_size->size_size.'号';
            $keyword_arr[]=$this_keyword;
            $this_suf_pro_code=$this_color->color_pro_code.$this_size->size_pro_code;
            $suf_pro_code_arr[]=$this_suf_pro_code;
            //var_dump($this_color);
            //var_dump($this_size);
        }
        //var_dump($cosir_id_arr);
        //var_dump($keyword_arr);
        //var_dump($suf_pro_code_arr);
        //轮播图片1
        $cp_filename=time().rand(1000,9999);
        if($request->file('carouse_pic1')->isValid())
        {
            $cp_ext=$request->file('carouse_pic1')->getClientOriginalExtension();
            $cp_basename=$cp_filename.'_1.'.$cp_ext;
            $request->file('carouse_pic1')->move('uploads/product/carouse_pic',$cp_basename);
            //等比缩放
            $fname='uploads/product/carouse_pic/'.$cp_basename;
            //var_dump($fname);
            $srcarr = getimagesize($fname);
            //var_dump($srcarr);
            $w=400;
            $h=400;
            if($w/$srcarr[0]>$h/$srcarr[1]){
                $bili = $h/$srcarr[1];
            }else{
                $bili = $w/$srcarr[0];
            }
            $nw=floor($srcarr[0]*$bili);
            $nh=floor($srcarr[1]*$bili);
            $img=Image::make($fname)->resize($nw,$nh)->save($fname);
            //缩放
            //var_dump($img);
        }
        if($request->file('carouse_pic2')->isValid())
        {
            $cp_ext=$request->file('carouse_pic2')->getClientOriginalExtension();
            $cp_basename=$cp_filename.'_2.'.$cp_ext;
            $request->file('carouse_pic2')->move('uploads/product/carouse_pic',$cp_basename);
            //等比缩放
            $fname='uploads/product/carouse_pic/'.$cp_basename;
            //var_dump($fname);
            $srcarr = getimagesize($fname);
            //var_dump($srcarr);
            $w=400;
            $h=400;
            if($w/$srcarr[0]>$h/$srcarr[1]){
                $bili = $h/$srcarr[1];
            }else{
                $bili = $w/$srcarr[0];
            }
            $nw=floor($srcarr[0]*$bili);
            $nh=floor($srcarr[1]*$bili);
            $img=Image::make($fname)->resize($nw,$nh)->save($fname);
            //缩放
            //var_dump($img);
        }
        if($request->file('carouse_pic3')->isValid())
        {
            $cp_ext=$request->file('carouse_pic3')->getClientOriginalExtension();
            $cp_basename=$cp_filename.'_3.'.$cp_ext;
            $request->file('carouse_pic3')->move('uploads/product/carouse_pic',$cp_basename);
            //等比缩放
            $fname='uploads/product/carouse_pic/'.$cp_basename;
            //var_dump($fname);
            $srcarr = getimagesize($fname);
            //var_dump($srcarr);
            $w=400;
            $h=400;
            if($w/$srcarr[0]>$h/$srcarr[1]){
                $bili = $h/$srcarr[1];
            }else{
                $bili = $w/$srcarr[0];
            }
            $nw=floor($srcarr[0]*$bili);
            $nh=floor($srcarr[1]*$bili);
            $img=Image::make($fname)->resize($nw,$nh)->save($fname);
            //缩放
            //var_dump($img);
        }
        if($request->file('carouse_pic4')->isValid())
        {
            $cp_ext=$request->file('carouse_pic4')->getClientOriginalExtension();
            $cp_basename=$cp_filename.'_4.'.$cp_ext;
            $request->file('carouse_pic4')->move('uploads/product/carouse_pic',$cp_basename);
            //等比缩放
            $fname='uploads/product/carouse_pic/'.$cp_basename;
            //var_dump($fname);
            $srcarr = getimagesize($fname);
            //var_dump($srcarr);
            $w=400;
            $h=400;
            if($w/$srcarr[0]>$h/$srcarr[1]){
                $bili = $h/$srcarr[1];
            }else{
                $bili = $w/$srcarr[0];
            }
            $nw=floor($srcarr[0]*$bili);
            $nh=floor($srcarr[1]*$bili);
            $img=Image::make($fname)->resize($nw,$nh)->save($fname);
            //缩放
            //var_dump($img);
        }
        //var_dump($cosir_id_arr);
        //var_dump($keyword_arr);
        //var_dump($suf_pro_code_arr);
        //var_dump($cp_filename);
        //var_dump(count($cosir_id_arr));
        for($i=0;$i<=count($cosir_id_arr)-1;$i++){
            DB::table('pro_attr')->insert(['pro_id'=>$inputs['pro_id'],'cosir_id'=>$cosir_id_arr[$i],'keyword'=>$keyword_arr[$i],'suf_pic'=>$cp_filename,'attr_stocknum'=>$inputs['attr_stocknum'][$i],'suf_pro_code'=>$suf_pro_code_arr[$i]]);
        }
        //此色系总库存
        $attr_stocknums=array_sum($inputs['attr_stocknum']);
        //var_dump($attr_stocknums);
        DB::table('product')->increment('stocknum',$attr_stocknums);
        return redirect('admin/productattr/'.$inputs['pro_id']);
        
    }



    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($pro_id,$cosir_id)
    {
        //echo $pro_id.'--'.$cosir_id;
        $data=DB::table('pro_attr')->where('pro_id','=',$pro_id)->where('cosir_id','=',$cosir_id)->first();
        //var_dump($data);
        return view('admin/product/product/editattr',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $inputs=$request->except(['_token']);
        //var_dump($inputs);

        DB::table('pro_attr')->where('pro_id','=',$inputs['pro_id'])->where('cosir_id','=',$inputs['cosir_id'])->update(['attr_stocknum'=>$inputs['stocknum']]);
        if($inputs['old_stocknum']>=$inputs['stocknum']){
            $changenum=$inputs['old_stocknum']-$inputs['stocknum'];
            DB::table('product')->where('product_id','=',$inputs['pro_id'])->decrement('stocknum',$changenum);
        }else{
            $changenum=$inputs['stocknum']-$inputs['old_stocknum'];
            DB::table('product')->where('product_id','=',$inputs['pro_id'])->increment('stocknum',$changenum);

        }
        return redirect('admin/product');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
